<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="资源任务单号：" prop="iNumber">
        <el-input
          v-model="queryParams.iNumber"
          placeholder="请输入资源任务单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源任务类型：" prop="iType">
      <el-radio-group v-model="queryParams.iType" >
     <el-radio :label='0' @click.native.prevent="changeRadio(0)">资源提供单</el-radio>
     <el-radio :label='1' @click.native.prevent="changeRadio(1)">资源需求单</el-radio>
      </el-radio-group>
      </el-form-item>

      
    
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    

   
    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资源信息任务单号" align="center" prop="iNumber" />
      <el-table-column label="资源信息任务类型" align="center"  :formatter="typeFormat" prop="iType"/>
      <el-table-column label="企业名称" align="center" prop="iEntName" />
      <el-table-column label="企业管理资源类型" align="center" prop="iResType" />
      <el-table-column label="企业管理资源数量" align="center" prop="iResNum" />
      <el-table-column label="企业的最低价" align="center"  prop="iPriLow" />
      <el-table-column label="企业的最高价" align="center"  prop="iPriHigh" />
      <el-table-column label="操作" align="center" prop="iResType" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-bell"
            @click="handleUpdate(scope.row)"
          >{{typename(scope.row.iResType)}}</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >等待交易中</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资源信息任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <p>
            资源信息任务单号：{{form.iNumber}}
          </p>
          <p>
            对方企业名称：比亚迪
          </p>
          <p>
            对方所期待交易的企业资源类型：{{form.iResType}}
          </p>
          <p>
            对方所期待交易的企业资源数量：{{form.iResNum}}
          </p>
          <p>
            你所期待的资源成交最低价为：{{form.iPriLow}}
          </p>
          <p>
            你所期待的资源成交最高价为：{{form.iPriHigh}}
          </p>
          <el-row>
          <el-col :span="10"> 现在你期待的资源成交价为：</el-col>
          <el-col :span="14"><el-input v-model="form.iResNussm"  size="small" placeholder="请输入你期待的资源成交价" /></el-col>
        </el-row>
        </el-row>
       
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/Resource/task";
import * as echarts from 'echarts';

export default {
  
  name: "Task",
  data() {
    return {

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资源信息任务表格数据
      taskList: [],
      //task
      tasknum:0,
      taskarr:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iType: null,
        iNumber: null,
        iEntName: '宁德时代',
        iResType: null,
        iResNum: null,
        iPriLow: null,
        iPriHigh: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.taskInfo();
  },
  mounted() {
    this.getData();

  },

  methods: {
  
    /** redio取消选中*/
   changeRadio(e){
    e===this.queryParams.iType?this.queryParams.iType=null:this.queryParams.iType=e
   },
    /** 任务类型转换 */
  
    typeFormat(row){
      if(row.iType=='0')return '资源提供单'
      else  return '资源需求单'
      },

    typef(type){
      if(type=='0')return '资源提供单'
      else  return '资源需求单'  
    },
    typename(type){
      console.log(type);
      if(type=='铁') return '交易中'
      else  return '等待交易中'  
    },
    /** 查询资源信息任务列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iId: null,
        iType: null,
        iNumber: null,
        iEntName: null,
        iResType: null,
        iResNum: null,
        iPriLow: null,
        iPriHigh: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.iId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资源信息任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const iId = row.iId || this.ids
      getTask(iId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "资源信息单交易详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.iId != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getData();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getData();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const iIds = row.iId || this.ids;
      this.$modal.confirm('是否确认删除资源信息任务编号为"' + iIds + '"的数据项？').then(function() {
        return delTask(iIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    async taskInfo(){
      await listTask(this.queryParams).then(response => {
        
        for (var i=0;i<response.rows.length;i++){
          this.taskarr.push(response.rows[i]);
          this.tasknum++;
        }
      //  console.log(scaleData1);
      });
    },
    
    getData(){
      listTask(this.queryParams).then(response => {
        const data=response.rows;
        this.renderChart1(data);
        this.renderChart2(data);
      }).catch(error=>{console.log('Error'+error)});
    },

    renderChart1(data) {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      var taskarr=data;
      let scaleLegend=['资源需求单','资源提供单'];
      var tasknum=taskarr.length;
      var tgd=0;
      for (var i=0;i<tasknum;i++){
        if (taskarr[i].iType=='0')
        tgd++;
      }
      var xqd=tasknum-tgd;
      var xqd=tasknum-tgd;
      const option = {
        title: {
          left: 'right',
      
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          data:scaleLegend,
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: '50%',
            data: [
            { value: tgd, name: '资源提供单',itemStyle: { color: '#FF0000' } },
              { value: xqd, name: '资源需求单',itemStyle: { color: '#FFA500' } }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0.5, 0.1, 0, 0.5)'
              }
            }
          }
        ]
      };
      option &&myChart.setOption(option);
      
  },

  renderChart2(data){
      const chartDom1 = this.$refs.chart1;
      const myChart1 = echarts.init(chartDom1);
      var taskarr=data;
      var taskmap=new Map();

      for (var i=0;i<taskarr.length;i++){
       if(taskmap.has(taskarr[i].iEntName)){
        var e=taskmap.get(taskarr[i].iEntName);
        taskmap.set(taskarr[i].iEntName,e+1);
       }else{
        taskmap.set(taskarr[i].iEntName,1);
       }
      }
      var arrayObj=Array.from(taskmap);
      arrayObj.sort(function(a,b){return a[1]-b[1]});
      taskmap = new Map(arrayObj.map(i => [i[0], i[1]]));
      var taskx=[];
      var tasky=[];
      taskmap.forEach((value, key)=>{
        taskx.push(key);
        tasky.push(value);  
        
      });
      
      const option = {
        xAxis: {
       type: 'category',
      data: taskx
      },
    yAxis: {
    type: 'value',
    splitNumber:4
     },
     series: [
    {
      data: tasky,
      type: 'bar',
      showBackground: true,
      backgroundStyle: {
        color: 'rgba(180, 180, 180, 0.2)'
      }
    }
  ]
      };

      option&&myChart1.setOption(option);
    },

  
}
};
</script>
